package org.nerve.webmagic.task;

import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;
import org.nerve.webmagic.bean.Data;
import org.nerve.webmagic.bean.WebPage;
import org.nerve.webmagic.pool.CounterPool;
import org.nerve.webmagic.pool.DataPool;
import org.nerve.webmagic.utils.DirUtil;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.TimerTask;

/**
 * org.nerve.webmagic.task
 * Created by zengxm on 2016/4/21 0021.
 */
public class PageConsumer extends TimerTask {

	private String getFileName(WebPage wp){
		return DirUtil.PATH+ File.separator+"originHtmls"+File.separator+
				wp.getDomain()+File.separator+
				(StringUtils.isNotBlank(wp.getDriver())?wp.getDriver()+File.separator:"")+
				System.currentTimeMillis() + ".json";
	}
	private void saveToFile(WebPage wp){
		File file=new File(getFileName(wp));
		try {
			if(!file.getParentFile().exists())
				file.getParentFile().mkdirs();
			PrintWriter e = new PrintWriter(new FileWriter(file));
			e.write(JSON.toJSONString(wp));
			e.close();
		} catch (IOException var5) {
			System.err.println("fail to save file "+file.getAbsolutePath()+",error="+var5.getMessage());
		}
	}

	@Override
	public void run() {
		try {
			Data data=DataPool.take();
			if(data instanceof WebPage){
				WebPage wp=(WebPage)data;
				System.out.println("[ThreadName="+Thread.currentThread().getName()+"]consume data: "+"title="+wp.getTitle()+",url="+wp.getUrl());

				//进行下一步的操作，通常是回传到数据接收器
				CounterPool.addBack();

				//保存到文件
				saveToFile(wp);
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
